const { ListNode } = require('./base')
const mergeTwoLists = function (list1, list2) {
  let dummy = new ListNode(-1)
  let p = dummy
  let p1 = list1
  let p2 = list2
  while (p1 && p2) {
    if (p1.val < p2.val) {
      p.next = p1
      p1 = p1.next
    } else {
      p.next = p2
      p2 = p2.next
    }
    p = p.next
  }
  if (p1) {
    p.next = p1
  } else if (p2) {
    p.next = p2
  }
  return dummy.next
}
// list1
let node3 = new ListNode(4)
let node2 = new ListNode(2, node3)
let node1 = new ListNode(1, node2)

// list2
let node6 = new ListNode(4)
let node5 = new ListNode(3, node6)
let node4 = new ListNode(1, node5)

let list1 = node1
let list2 = node4

console.log(list1, list2)

let res = mergeTwoLists(list1, list2)
while (res) {
  console.log(res.val, '->')
  res = res.next
}
